﻿Imports System.Data.SqlClient

Public Class clsOrdenDiesel
    Private mintIdOrden As Integer
    Private mintIdAutobus As Integer
    Private mintIdRuta As Integer
    Private mintIdOperador As Integer
    Private mdblLstOrden As Double
    Private mdblLstDesp As Double
    Private mstrObservaciones As String
    Private mdatFechaOrden As Date
    Private mdatFechaSurtido As Date
    Private mstrUsuario As String
    Private mstrStatus As String
    Public Property IdOrden() As Integer
        Get
            Return mintIdOrden
        End Get
        Set(ByVal value As Integer)
            mintIdOrden = value
        End Set
    End Property
    Public Property IdAutobus() As Integer
        Get
            Return mintIdAutobus
        End Get
        Set(ByVal value As Integer)
            mintIdAutobus = value
        End Set
    End Property
    Public Property IdRuta() As Integer
        Get
            Return mintIdRuta
        End Get
        Set(ByVal value As Integer)
            mintIdRuta = value
        End Set
    End Property
    Public Property IdOperador() As Integer
        Get
            Return mintIdOperador
        End Get
        Set(ByVal value As Integer)
            mintIdOperador = value
        End Set
    End Property
    Public Property LtsOrden As Double
        Get
            Return mdblLstOrden
        End Get
        Set(ByVal value As Double)
            mdblLstOrden = value
        End Set
    End Property
    Public Property LtsDesp As Double
        Get
            Return mdblLstDesp
        End Get
        Set(ByVal value As Double)
            mdblLstDesp = value
        End Set
    End Property
    Public Property Observaciones() As String
        Get
            Return mstrObservaciones
        End Get
        Set(ByVal value As String)
            mstrObservaciones = value
        End Set
    End Property
    Public Property FechaOrden() As Date
        Get
            Return mdatFechaOrden
        End Get
        Set(ByVal value As Date)
            mdatFechaOrden = value
        End Set
    End Property
    Public Property FechaSurtido() As Date
        Get
            Return mdatFechaSurtido
        End Get
        Set(ByVal value As Date)
            mdatFechaSurtido = value
        End Set
    End Property
   
    Public Property Usuario() As String
        Get
            Return mstrUsuario
        End Get
        Set(ByVal value As String)
            mstrUsuario = value
        End Set
    End Property
    Public Property Status() As String
        Get
            Return mstrStatus
        End Get
        Set(ByVal value As String)
            mstrStatus = value
        End Set
    End Property
    Public Sub New()
        mintIdOrden = 0
        mintIdAutobus = 0
        mintIdRuta = 0
        mintIdOperador = 0
        mdblLstOrden = 0
        mdblLstDesp = 0
        mstrObservaciones = ""
        mdatFechaOrden = Now()
        mdatFechaSurtido = Now()
        mstrUsuario = ""
        mstrStatus = ""
    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into OrdenDiesel (" & _
                            "IdAutobus," & _
                            "IdRuta," & _
                            "IdOperador," & _
                            "LstOrden," & _
                            "LstDesp," & _
                            "Observaciones," & _
                            "FechaOrden," & _
                            "FechaSurtido," & _
                            "Usuario," & _
                            "Status)" & _
                            "VALUES (" & _
                            "@IdAutobus," & _
                            "@IdRuta," & _
                            "@IdOperador," & _
                            "@LstOrden," & _
                            "@LstDesp," & _
                            "substring(@Observaciones,1,500)," & _
                            "@FechaOrden," & _
                            "@FechaSurtido," & _
                            "substring(@Usuario,1,50)," & _
                            "substring(@Status,1,1))" & _
                            " SELECT SCOPE_IDENTITY()"


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdAutobus", mintIdAutobus)
        Command.Parameters.AddWithValue("IdRuta", mintIdRuta)
        Command.Parameters.AddWithValue("IdOperador", mintIdOperador)
        Command.Parameters.AddWithValue("LstOrden", mdblLstOrden)
        Command.Parameters.AddWithValue("LstDesp", mdblLstDesp)
        Command.Parameters.AddWithValue("Observaciones", mstrObservaciones)
        Command.Parameters.AddWithValue("FechaOrden", mdatFechaOrden)
        Command.Parameters.AddWithValue("FechaSurtido", mdatFechaSurtido)
        Command.Parameters.AddWithValue("Usuario", mstrUsuario)
        Command.Parameters.AddWithValue("Status", mstrStatus)
        
        cnConn.Open()
        Try
            IdOrden = Convert.ToInt32(Command.ExecuteScalar)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Aviso")
        End Try


        If IdOrden > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar la orden de diesel ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update OrdenDiesel Set " & _
                    "IdRuta=@IdRuta," & _
                    "IdOperador=@IdOperador," & _
                    "LstOrden=@LstOrden " & _
                    "Where IdOrden=@IdOrden"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("IdOrden", mintIdOrden)
        Command.Parameters.AddWithValue("IdAutobus", mintIdAutobus)
        Command.Parameters.AddWithValue("IdRuta", mintIdRuta)
        Command.Parameters.AddWithValue("IdOperador", mintIdOperador)
        Command.Parameters.AddWithValue("LstOrden", mdblLstOrden)
        Command.Parameters.AddWithValue("LstDesp", mdblLstDesp)
        Command.Parameters.AddWithValue("Observaciones", mstrObservaciones)
        Command.Parameters.AddWithValue("FechaOrden", mdatFechaOrden)
        Command.Parameters.AddWithValue("FechaSurtido", mdatFechaSurtido)
        Command.Parameters.AddWithValue("Usuario", mstrUsuario)
        Command.Parameters.AddWithValue("Status", mstrUsuario)
        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar la orden ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
    Public Sub ActualizarCarga()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = CitraConnection
        strSqlQry = "Update OrdenDiesel Set " & _
                    "Status='S'," & _
                    "LstDesp=@LstDesp," & _
                    "FechaSurtido=@FechaSurtido " & _
                    "Where IdOrden=@IdOrden"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("IdOrden", mintIdOrden)
        Command.Parameters.AddWithValue("IdAutobus", mintIdAutobus)
        Command.Parameters.AddWithValue("IdRuta", mintIdRuta)
        Command.Parameters.AddWithValue("IdOperador", mintIdOperador)
        Command.Parameters.AddWithValue("LstOrden", mdblLstOrden)
        Command.Parameters.AddWithValue("LstDesp", mdblLstDesp)
        Command.Parameters.AddWithValue("Observaciones", mstrObservaciones)
        Command.Parameters.AddWithValue("FechaOrden", mdatFechaOrden)
        Command.Parameters.AddWithValue("FechaSurtido", mdatFechaSurtido)
        Command.Parameters.AddWithValue("Usuario", mstrUsuario)
        Command.Parameters.AddWithValue("Status", mstrUsuario)
        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar la orden ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If



    End Sub
End Class
